<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const a = {}
    a.b = a
    console.log(a);
    function hasLoop (obj) {
      // 判断对象内部是否有和源相同的属性
      function findLoop (target, src) {
        // 源数组，并将自身传入
        const source = src.slice().concat([target])
        for (const key in target) {
          // 如果是对象才需要判断
          if (typeof target[key] === 'object') {
            // 如果在源数组中找到 || 递归查找内部属性找到相同
            if (source.indexOf(target[key]) > -1 || findLoop(target[key], source)) {
              return true
            }
          }
        }
        return false
      }
      // 如果传入值是对象，则执行判断，否则返回false
      return typeof obj === 'object' ? findLoop(obj, []) : false
    }
    console.log(hasLoop(a));
  </script>
</body>

</html>